package com.myj.jim.util;

import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * MD5相关的处理类
 * @author shun
 */
public class MD5Utils {
	private static final Logger log = LoggerFactory.getLogger(MD5Utils.class);
	
	public static String encode(String value){
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				  'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = value.getBytes();
			// 使用MD5创建MessageDigest对象
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte b = md[i];
				// System.out.println((int)b);
				// 将没个数(int)b进行双字节加密
				str[k++] = hexDigits[b >> 4 & 0xf];
				str[k++] = hexDigits[b & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			log.error("encode MD5 error.", e);
			return null;
		}
		
	}
	
}
